package cn.edu.ctbu.dp01.service;

import cn.edu.ctbu.dp01.dao.StudentRepository;
import cn.edu.ctbu.dp01.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import  java.util.List;

@Service

public class StudentService {
    @Autowired
    public StudentRepository studentRepository;

    //读取数据库所有列表
    public List<Student> findALL() {

        return studentRepository.findAll();

    }

    //按id进行查询
    public Student findById(Integer id) {
        return studentRepository.findById(id).orElse(null);
    }

    //按名字进行查询
    public List<Student> findByName(String name) {

        return studentRepository.findByNameLike(name);

    }
    //按名字和密码进行查询
    public List<Student> findByNameAndSno(String name,String sno) {

        return studentRepository.findByNameAndSno(name,sno);

    }
    //新增
    public void Insert(Student student) {
        studentRepository.save(student);
    }
    //更新
    public void Update(Student student) {
        studentRepository.save(student);
    }
    //删除
    public void delete(Integer id) {
        studentRepository.deleteById(id);
    }
    //最大的学号
    public Student findStudentWithMaxSno() {

        return studentRepository.findStudentWithMaxSno();
    }
    //新增学生
    public Student add(Student student) {
        return studentRepository.save(student);
    }
    //更新学生
    public Student update(Student student) {
        return studentRepository.save(student);
    }

    //按学号查询
    public Student findById(int id) {
        return studentRepository.findById(id).get();
    }


}

